package com.cskaoyan.controller.backgroundsystemcontroller;


import com.cskaoyan.bean.bo.TopicListBo;
import com.cskaoyan.bean.pojo.Topic;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.TopicListVo;
import com.cskaoyan.service.PromotionService;
import com.cskaoyan.utils.InvalidatedParamsUtil;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author: lsy
 * @class: TopicController
 */
@RestController
@RequestMapping("/admin/topic")
public class TopicController {

    @Autowired
    PromotionService promotionService;

    /**
     * @author: lsy
     * @title: list
     * @description: 分页查询专题信息
     * @param: [topicListBo]
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @GetMapping("list")
    @RequiresPermissions(value = {"*","admin:topic:list" },logical = Logical.OR)
    public BaseRespVo list(TopicListBo topicListBo) {
        TopicListVo topicListVo = promotionService.queryTopics(topicListBo);
        return BaseRespVo.ok(topicListVo);
    }

    /**
     * @author: lsy
     * @title: update
     * @description: 修改专题信息
     * @param: [topic]
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @PostMapping("update")
    @RequiresPermissions(value = {"*","admin:topic:update" },logical = Logical.OR)
    public BaseRespVo update(@RequestBody @Validated Topic topic, BindingResult result) {
        if (result.hasErrors()) {
            return InvalidatedParamsUtil.resErrorVo(result);
        }
        promotionService.changeTopic(topic);
        return BaseRespVo.ok();
    }

    /**
     * @author: lsy
     * @title: delete
     * @description: 删除专题
     * @param: [topic]
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @PostMapping("delete")
    @RequiresPermissions(value = {"*","admin:topic:delete" },logical = Logical.OR)
    public BaseRespVo delete(@RequestBody Topic topic) {
        Topic topic1 = new Topic();
        topic1.setId(topic.getId());
        topic1.setDeleted(true);
        promotionService.changeTopic(topic1);
        return BaseRespVo.ok();

    }


    /**
     * @author: lsy
     * @title: create
     * @description: 新增专题信息
     * @param: [topic]
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @PostMapping("create")
    @RequiresPermissions(value = {"*","admin:topic:create" },logical = Logical.OR)
    public BaseRespVo create(@RequestBody @Validated Topic topic, BindingResult result) {
        if (result.hasErrors()) {
            return InvalidatedParamsUtil.resErrorVo(result);
        }
        topic.setDeleted(false);
        promotionService.addTopic(topic);
        return BaseRespVo.ok();
   }
}
